/**
 * Copyright 2006 Brad Reynolds
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.leeloo.databinding.internal.ui;

import junit.framework.TestCase;

import org.leeloo.core.Tag;
import org.leeloo.core.TagService;
import org.leeloo.databinding.ui.descriptor.BindingEventDescriptor;
import org.leeloo.databinding.ui.descriptor.DiffDescriptor;
import org.leeloo.databinding.ui.descriptor.ObservableDescriptor;
import org.leeloo.databinding.ui.event.FireBindingEventEvent;
import org.leeloo.internal.core.ObjectDescriptor;

public class TagFilterTest extends TestCase {
    private FireBindingEventEvent event;
    private TagService tagService;
//    private Object target;
    private Tag tag;
    private TagFilter filter;
    
    protected void setUp() throws Exception {
        tagService = new TagService();
        int id = 0;

        event = new FireBindingEventEvent();
        
        ObjectDescriptor binding = new ObjectDescriptor();
        binding.setIdentityHashCode(id++);
        binding.setClassName("binding");
        event.setBindingDescriptor(binding);
        
        BindingEventDescriptor bindingEvent = new BindingEventDescriptor();
        bindingEvent.setIdentityHashCode(id++);
        bindingEvent.setClassName("bindingEvent");
        event.setBindingEventDescriptor(bindingEvent);
        
        DiffDescriptor diff = new DiffDescriptor();
        diff.setClassName("diff");
        diff.setIdentityHashCode(id++);
        bindingEvent.setDiffDescriptor(diff);
        
        ObservableDescriptor targetObservable = new ObservableDescriptor();
        bindingEvent.setTargetObservableDescriptor(targetObservable);
        bindingEvent.setTargetObservableDescriptor(targetObservable);
        
        ObjectDescriptor target = new ObjectDescriptor();
        target.setIdentityHashCode(id++);
        target.setClassName("target");
        targetObservable.setObservedDescriptor(target);
        
        ObservableDescriptor modelObservable = new ObservableDescriptor();
        modelObservable.setIdentityHashCode(id++);
        modelObservable.setClassName("modelObservable");
        bindingEvent.setModelObservableDescriptor(modelObservable);
        
        ObjectDescriptor model = new ObservableDescriptor();
        model.setIdentityHashCode(id++);
        model.setClassName("model");
        modelObservable.setObservedDescriptor(model);
        
        tag = new Tag("name", "value");
        filter = new TagFilter(tagService, tag);        
    }
    
    public void testIsFilterProperty() throws Exception {
        TagFilter filter = new TagFilter(tagService, new Tag("dummy", "dummy"));
        assertTrue(filter.isFilterProperty(event, "tags"));
    }
    
    public void testFilterForBinding() throws Exception {
        tagService.put(event.getBindingDescriptor(), tag);
        assertTrue(filter.select(null, null, event));
    }
    
    public void testFilterForTarget() throws Exception {
        tagService.put(event.getBindingEventDescriptor().getTargetObservableDescriptor().getObservedDescriptor(), tag);
        assertTrue(filter.select(null, null, event));
    }
    
    public void testFilterForBindingEvent() throws Exception {
        tagService.put(event.getBindingEventDescriptor(), tag);
        assertTrue(filter.select(null, null, event));
    }
    
    public void testFilterForDiff() throws Exception {
        tagService.put(event.getBindingEventDescriptor().getDiffDescriptor(), tag);
        assertTrue(filter.select(null, null, event));
        
    }
    
    public void testFilterForTargetObservable() throws Exception {
        tagService.put(event.getBindingEventDescriptor().getTargetObservableDescriptor(), tag);
        assertTrue(filter.select(null, null, event));
    }
    
    public void testFilterForModelObservable() throws Exception {
        tagService.put(event.getBindingEventDescriptor().getModelObservableDescriptor(), tag);
        assertTrue(filter.select(null, null, event));
    }
    
    public void testFilterForModel() throws Exception {
        tagService.put(event.getBindingEventDescriptor().getModelObservableDescriptor().getObservedDescriptor(), tag);
        assertTrue(filter.select(null, null, event));
    }
}
